// Whitemice.MOGI.RPC2.TaskAnnotation
// License: LGPL
// Copyright: Whitemice Consulting, 2006
// Author(s): Adam Tauno Williams (awilliam@whitemice.org)
using System;
using System.Globalization;
using Whitemice.MOGI;
using CookComputing.XmlRpc;

namespace Whitemice.MOGI.RPC2 {
	/// \brief Implementation of ITaskAnnotation
	public class TaskAnnotation : 
		Whitemice.MOGI.RPC2.Annotation, ITaskAnnotation 
	{
	
		/// \brief Constructor for existing annotation
		/// \param document	XmlRpcStruct of existing annotation
		/// \param server	Reference to server object
		public TaskAnnotation(XmlRpcStruct document, Server server) 
			: base(document, server) 
		{
			server.Log.Debug("TaskAnnotation(XmlRpcStruct, Server)");
			documentId = "Document/Task/ " + RPC2.ShortId((string)document["jobId"]);
			server.Log.Debug("TaskAnnotation(XmlRpcStruct, Server) -  documentId = " 
				+ documentId);
		}
		
		/// \brief Constructor for a NEW task annotation
		/// \param documentId	The MOGI document id of the task being annotated
		/// \param server		Reference to the server object
		/// \note  The annotation date/time is set to the time of this objects 
		/// creation, this will get overwritten when the document is actually 
		/// posted to the server.  The action defaults to ETaskStatus.Commented
		public TaskAnnotation(string documentId, Server server) 
			: base(documentId, server) 
		{
			server.Log.Debug("TaskAnnotation(string, Server)");
			document.Add("comment", "");
			document.Add("action", "10_commented");
			document.Add("actionDate", DateTime.Now);
		}
		
		/// \brief Return the name of the annotation
		public override string Name 
		{
			get 
			{
				Log.Debug("Name ==");
				if (document.Contains("id"))
					return "Annotation/Task/" + MOGI.Id(documentId) 
						+ "/"+ RPC2.ShortId((string)document["id"]);
				return "Annotation/Task/" + MOGI.Id(documentId) + "/0";
			}
		}
		
		/// \brief Return/Update the text of the annotation
		public override string Comment
		{
			get { return (string)document["comment"]; }
			set { document["comment"] = value; }
		}
		
		/// \brief Return the MOGI Id of the annotation's creator
		/// \todo This should return the current actor if annotation has not 
		/// been posted
		public string Actor
		{
			get 
			{
				return "Document/Account/" +
					RPC2.ShortId((string)document["actorId"]); 
			}
		}
		
		/// \brief Return the datetime of the annotation if the annotation has 
		/// been posted, otherwise it returns the value stored in this element 
		/// when the document was created.
		public override DateTime Date
		{
			get { return (DateTime)document["actionDate"]; }
		}
		
		/// \brief Turns an action keys string into an ETaskStatus value
		/// \warn Only some of these statuses are actually useful
		protected ETaskStatus GetStatus(string key)
		{
			if (document.Contains(key))
	  			switch (document[key].ToString()) 
	  			{
					case "00_created": return ETaskStatus.Created;
					case "02_rejected": return ETaskStatus.Rejected;
					case "05_accepted": return ETaskStatus.Accepted;
					case "10_archived": return ETaskStatus.Archived;
					case "10_commented": return ETaskStatus.Commented;
					case "15_divided": return ETaskStatus.Divided;
					case "20_processing": return ETaskStatus.Processing;
					case "25_done": return ETaskStatus.Done;
					case "27_reactivated": return ETaskStatus.Reactivated;
					case "30_archived": return ETaskStatus.Archived;
					default: return ETaskStatus.Created;
	  			}
	  			else return ETaskStatus.Created;
	 	}
	 	
	 	/// \brief Allow the type of annotation to be adjusted/retrieved
	 	public ETaskStatus Action
	 	{
	 		get { return GetStatus("action"); }
		 	set 
		 	{
				switch (value) 
		  	{
		   		case ETaskStatus.Created: 
		   				document["action"] = "00_created"; 
		   			break;
		   		case ETaskStatus.Rejected: 
		   				document["action"] = "02_rejected"; 
		   			break;
		   		case ETaskStatus.Accepted: 
		   				document["action"] = "05_accepted"; 
		   			break;
		   		case ETaskStatus.Archived: 
		   				document["action"] = "10_archived"; 
		   			break;
		   		case ETaskStatus.Commented: 
		   				document["action"] = "10_commented";
		   			break;
		   		case ETaskStatus.Divided: 
		   				document["action"] = "15_divided"; 
		   			break;
		   		case ETaskStatus.Processing: 
		   				document["action"] = "20_processing"; 
		   			break;
		   		case ETaskStatus.Done: 
		   				document["action"] = "25_done";	
		   			break;
		   		case ETaskStatus.Reactivated: 
		   				document["action"] = "27_reactivated"; 
		   			break;
		  	}
		 	}
		}
	
		/// \brief Return the status that this annotation left the job in
		/// \warn This is meaningless if the annotation has not been posted 
		///	to the server
		/// \todo Return undefined if this annotation has not been posted
		public ETaskStatus Status
		{
			get { return GetStatus("jobStatus"); }
		}
	}
}